using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Framework.Library.Components.Providers;
using Framework.Library.Utilities.Database;
using System.Data.SqlClient;
using System.Collections;

namespace Framework.Library.Components.Database
{    
    /// <summary>
    /// Tables manage class
    /// </summary>
    public class _Table : Info
    {
        #region "Variables"
        /// <summary>
        /// File group of the table
        /// </summary>
        private string _FileGroup;
        /// <summary>
        /// Table columns collection
        /// </summary>
        private Columns _ColumnsCollection;

        #endregion

        #region "Properties"        
        /// <summary>
        /// Table file group
        /// </summary>
        public string FileGroup
        {
            get
            {
                return _FileGroup;
            }
            set
            {
                _FileGroup = value;
            }
        }
        /// <summary>
        /// Table columns collection
        /// </summary>
        public Columns Columns
        {
            get
            {
                return _ColumnsCollection;
            }
        }
        #endregion
        /// <summary>
        /// Constructor
        /// </summary>
        public _Table()
        {
            _ColumnsCollection = new Columns();
        }
        /// <summary>
        /// Get all Tables
        /// </summary>
        public DataTable GetTables()
        {
            DataTable dtCommands = DataAccessProvider.Instance().GetTables();
            return dtCommands;
        }
        /// <summary>
        /// Drop the table
        /// </summary>
        public void DropTable(string tblName)
        {
            DataAccessProvider.Instance().DropTable(tblName);
        }
        /// <summary>
        /// Load table properties, afert setting the object ID
        /// </summary>
        public void LoadProperties()
        {
            SqlDataReader myReader = DataAccessProvider.Instance().LoadProperty(ID);

            while (myReader.Read())
            {
                this.Name = myReader.GetString(0);
                this.Owner = myReader.GetString(1);
            }
        }
        /// <summary>
        /// drop default constraint
        /// </summary>
        /// <param name="defaultConstraint">Default constraint name</param>
        public void DropDefaultConstraint(string defaultConstraint)
        {
            string _sqlCommand = "";
            _sqlCommand = "ALTER TABLE " + this.GetObjectName() + " DROP CONSTRAINT " + defaultConstraint;
            DataAccessProvider.Instance().ExecuteCommand(_sqlCommand);
        }
        /// <summary>
        /// Unbinding table column
        /// </summary>
        /// <param name="columnName">Column name</param>
        public void DropDefaultBinding(string columnName)
        {
            string _sqlCommand = "";
            _sqlCommand = "EXEC sp_unbindefault '" + this.GetObjectName().Replace("'", "''") + "." + columnName + "'";
            DataAccessProvider.Instance().ExecuteCommand(_sqlCommand);
        }
        /// <summary>
        /// drop table column
        /// </summary>
        /// <param name="objColumn">Column object</param>
        public void DropColumn(_Column objColumn)
        {
            string _sqlCommand = "";

            //if there is a binding default is necessary to unbind
            if (objColumn.DefaultBinding != "")
            {
                _sqlCommand = "EXEC sp_unbindefault '" + this.GetObjectName().Replace("'", "''") + ".[" + objColumn.Name.Replace("'", "''") + "]'";

                //System.Web.HttpContext.Current.Response.Write(_sqlCommand);
                DataAccessProvider.Instance().ExecuteCommand(_sqlCommand);
            }
            if (objColumn.DefaultConstraint != "" && objColumn.DefaultBinding == "")
            {
                //if is default delete the constraint
                _sqlCommand = "ALTER TABLE " + this.GetObjectName() + " DROP CONSTRAINT " + objColumn.DefaultConstraint;
                //System.Web.HttpContext.Current.Response.Write(_sqlCommand);
                DataAccessProvider.Instance().ExecuteCommand(_sqlCommand);
            }

            //Drop column
            _sqlCommand = "ALTER TABLE " + this.GetObjectName() + " DROP COLUMN [" + objColumn.Name + "]";
            //System.Web.HttpContext.Current.Response.Write(_sqlCommand);
            DataAccessProvider.Instance().ExecuteCommand(_sqlCommand);
        }
        /// <summary>
        /// add table column
        /// </summary>
        /// <param name="objColumn">Column object</param>
        public void AddColumn(_Column objColumn)
        {
            string _sqlCommand = "";
            _sqlCommand += "ALTER TABLE " + this.GetObjectName() + "\r\n ADD ";
            //column name
            _sqlCommand += objColumn.Name;
            //column type
            _sqlCommand += " " + objColumn.Type;
            //add width and scale if there's
            if (objColumn.Width != "")
            {
                if (objColumn.Scale != "" && objColumn.Scale != "0")
                {
                    _sqlCommand += "(" + objColumn.Width + "," + objColumn.Scale + ")";
                }
                else
                {
                    _sqlCommand += "(" + objColumn.Width + ")";
                }
            }
            //if is identity
            if (objColumn.IsIdentity)
            {
                if (objColumn.Increment == "1" && objColumn.Seed == "1")
                {
                    _sqlCommand += " IDENTITY";
                }
                else
                {
                    _sqlCommand += " IDENTITY (" + objColumn.Seed + "," + objColumn.Increment + ")";
                }
            }
            else
            {
                //default value
                if (objColumn.Default != "")
                {
                    _sqlCommand += " DEFAULT " + objColumn.Default;
                }
                //Nulls
                if (objColumn.AllowNulls)
                {
                    _sqlCommand += " NULL";
                }
            }
            // if is not null is necessary alter the table
            if (!objColumn.AllowNulls)
            {
                _sqlCommand += ";\n\r";
                _sqlCommand += " ALTER TABLE " + this.GetObjectName();
                _sqlCommand += " ALTER COLUMN " + objColumn.Name;
                //column type
                _sqlCommand += " " + objColumn.Type;
                //add width and scale if there's
                if (objColumn.Width != "")
                {
                    if (objColumn.Scale != "" && objColumn.Scale != "0")
                    {
                        _sqlCommand += "(" + objColumn.Width + "," + objColumn.Scale + ")";
                    }
                    else
                    {
                        _sqlCommand += "(" + objColumn.Width + ")";
                    }
                }
                _sqlCommand += " NOT NULL";

            }
            if (objColumn.DefaultBinding != "")
            {
                _sqlCommand += ";\n\r";
                _sqlCommand += "EXEC sp_bindefault '" + objColumn.DefaultBinding + "', '" + this.GetObjectName().Replace("'", "''") + ".[" + objColumn.Name.Replace("'", "''") + "]'";
            }
            if (objColumn.RuleBinding != "")
            {
                _sqlCommand += ";\n\r";
                _sqlCommand += " EXEC sp_bindrule '" + objColumn.RuleBinding + "', '" + this.GetObjectName().Replace("'", "''") + ".[" + objColumn.Name.Replace("'", "''") + "]'";
            }

            //System.Web.HttpContext.Current.Response.Write(_sqlCommand);
            //Execute
            DataAccessProvider.Instance().ExecuteCommand(_sqlCommand);

        }
        /// <summary>
        /// update column DDL
        /// </summary>
        /// <param name="objColumn">Column object</param>
        public void UpdateColumn(_Column objColumn)
        {
            //rename
            if (objColumn.Name != objColumn.OldName)
            {
                RenameColumn(objColumn);
            }
            string _sqlCommand = "";
            _sqlCommand += "ALTER TABLE " + this.GetObjectName() + " ALTER COLUMN ";
            //column name
            _sqlCommand += objColumn.Name;
            //column type
            _sqlCommand += " " + objColumn.Type;
            //add width and scale if there's
            if (objColumn.Width != "")
            {
                if (objColumn.Scale != "" && objColumn.Scale != "0")
                {
                    _sqlCommand += "(" + objColumn.Width + "," + objColumn.Scale + ")";
                }
                else
                {
                    _sqlCommand += "(" + objColumn.Width + ")";
                }
            }
            //if is identity
            if (objColumn.IsIdentity)
            {
                if (objColumn.Increment == "1" && objColumn.Seed == "1")
                {
                    _sqlCommand += " IDENTITY";
                }
                else
                {
                    _sqlCommand += " IDENTITY (" + objColumn.Seed + "," + objColumn.Increment + ")";
                }
            }
            else
            {
                //Nulls
                if (objColumn.AllowNulls)
                {
                    _sqlCommand += " NULL";
                }
                else
                {
                    _sqlCommand += " NOT NULL";
                }
            }

            if (objColumn.Default != "")
            {
                _sqlCommand += ";\n\r";
                _sqlCommand += " ALTER TABLE " + this.GetObjectName() + " ADD DEFAULT " + objColumn.Default + " FOR " + objColumn.Name;
            }
            if (objColumn.DefaultBinding != "")
            {
                _sqlCommand += ";\n\r";
                _sqlCommand += " EXEC sp_bindefault '" + objColumn.DefaultBinding + "', '" + this.GetObjectName().Replace("'", "''") + ".[" + objColumn.Name.Replace("'", "''") + "]'";
            }
            if (objColumn.RuleBinding != "")
            {
                _sqlCommand += ";\n\r";
                _sqlCommand += " EXEC sp_bindrule '" + objColumn.RuleBinding + "', '" + this.GetObjectName().Replace("'", "''") + ".[" + objColumn.Name.Replace("'", "''") + "]'";
            }

            //System.Web.HttpContext.Current.Response.Write(_sqlCommand);
            //Execute
            DataAccessProvider.Instance().ExecuteCommand(_sqlCommand);
        }
        /// <summary>
        /// rename column
        /// </summary>
        /// <param name="objColumn">column object</param>
        public void RenameColumn(_Column objColumn)
        {
            string _sqlCommand = "";
            _sqlCommand += "EXEC sp_rename '" + this.GetObjectName().Replace("'", "''") + "." + objColumn.OldName + "', '" + objColumn.Name + "','COLUMN'";
            DataAccessProvider.Instance().ExecuteCommand(_sqlCommand);
        }
        /// <summary>
        /// alter, delete or add table column
        /// </summary>
        /// <param name="objColumn">Column object</param>
        /// <param name="operation">Operation (ADD, UPD, DEL)</param>
        public void AlterColumn(_Column objColumn, string operation)
        {
            switch (operation)
            {
                case "ADD":
                    AddColumn(objColumn);
                    break;
                case "UPD":
                    UpdateColumn(objColumn);
                    break;
                case "DEL":
                    DropColumn(objColumn);
                    break;
            }
        }
        /// <summary>
        /// Load the columns in the collection
        /// </summary>
        public void LoadColumns()
        {
            DataTable columnsDataTable = DataAccessProvider.Instance().GetColums(this.GetObjectName().Replace("'", "''"));
            SqlDataReader myDataReader;

            foreach (DataRow columnRow in columnsDataTable.Rows)
            {
                _Column newColumn = new _Column();
                newColumn.Name = columnRow["name"].ToString();
                newColumn.Type = columnRow["datatype"].ToString();
                newColumn.Width = columnRow["length"].ToString();
                newColumn.Scale = columnRow["scale"].ToString();
                newColumn.IsIdentity = Convert.ToBoolean(columnRow["identity"]);

                newColumn.DefaultConstraint = columnRow["defConstraint"].ToString();

                newColumn.AllowNulls = Convert.ToBoolean(columnRow["isnull"]);
                newColumn.ColId = columnRow["colid"].ToString();
                newColumn.Precision = columnRow["prec"].ToString();
                newColumn.RuleBinding = columnRow["ruleBind"].ToString();

                //Seed & increment
                newColumn.Seed = "0";
                newColumn.Increment = "0";
                if (newColumn.IsIdentity)
                {
                    myDataReader = DataAccessProvider.Instance().ReturnReader("SELECT IDENT_SEED('" + this.GetObjectName().Replace("'", "''") + "'),IDENT_INCR('" + this.GetObjectName().Replace("'", "''") + "') ");
                    while (myDataReader.Read())
                    {
                        newColumn.Seed = myDataReader.GetDecimal(0).ToString();
                        newColumn.Increment = myDataReader.GetDecimal(1).ToString();
                    }
                }

                //Primary key
                myDataReader = DataAccessProvider.Instance().ReturnReader("SELECT COL_NAME(id, colid) FROM sysindexkeys WHERE id=OBJECT_ID('" + this.GetObjectName().Replace("'", "''") + "') AND indid=1 AND COL_NAME(id, colid) = '" + newColumn.Name + "' ORDER BY keyno");
                if (myDataReader.HasRows)
                {
                    newColumn.IsPrimaryKey = true;
                }
                else
                {
                    newColumn.IsPrimaryKey = false;
                }

                //Default
                myDataReader = DataAccessProvider.Instance().ReturnReader("SELECT USER_NAME(O.uid)+'.'+O.name,M.text, isnull( (SELECT constid FROM sysconstraints where constid = O.id), 0) as sysconst FROM syscomments M,syscolumns C,sysobjects O WHERE C.id=OBJECT_ID('" + this.GetObjectName().Replace("'", "''") + "') and C.name=('" + newColumn.Name + "') AND O.type='D' AND M.id=C.cdefault AND M.id=O.id");
                if (myDataReader.HasRows)
                {
                    while (myDataReader.Read())
                    {
                        if (myDataReader.GetInt32(2) == 0)
                        {
                            newColumn.Default = "";
                            newColumn.DefaultBinding = myDataReader.GetString(0);
                        }
                        else
                        {
                            newColumn.Default = myDataReader.GetString(1).Trim('(', ')');
                            newColumn.DefaultBinding = "";
                        }
                    }
                }

                //newColumn.Size;			

                //Add to the collection
                _ColumnsCollection.Add(newColumn);
            }

            //return _columnsCollection;
        }
        /// <summary>
        /// Create new table
        /// </summary>
        public void Create()
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("CREATE TABLE " + this.GetObjectName() + "\r\n");
            strSQL.Append("(\r\n");
            foreach (_Column objColumn in _ColumnsCollection)
            {
                //column name
                strSQL.Append(objColumn.Name);
                //column type
                strSQL.Append(" " + objColumn.Type);
                //add width and scale if there's
                if (objColumn.Width != "")
                {
                    if (objColumn.Scale != "" && objColumn.Scale != "0")
                    {
                        strSQL.Append("(" + objColumn.Width + "," + objColumn.Scale + ")");
                    }
                    else
                    {
                        strSQL.Append("(" + objColumn.Width + ")");
                    }
                }
                //if is identity
                if (objColumn.IsIdentity)
                {
                    if (objColumn.Increment == "1" && objColumn.Seed == "1")
                    {
                        strSQL.Append(" IDENTITY");
                    }
                    else
                    {
                        strSQL.Append(" IDENTITY (" + objColumn.Seed + "," + objColumn.Increment + ")");
                    }
                }
                else
                {
                    //default value
                    if (objColumn.Default != "")
                    {
                        strSQL.Append(" DEFAULT " + objColumn.Default);
                    }
                    //Nulls
                    if (objColumn.AllowNulls)
                    {
                        strSQL.Append(" NULL");
                    }
                    else
                    {
                        strSQL.Append(" NOT NULL");
                    }
                }
                //end row
                strSQL.Append(",\r\n");
            }
            //delete the last comma, is not a great solution
            strSQL.Remove(strSQL.Length - 3, 3);
            //strSQL.Append(strSQL.ToString().Substring(0, strSQL.Length - 3));

            strSQL.Append("\r\n)\r\n");
            //filegroup
            if (_FileGroup != "Default")
            {
                strSQL.Append("ON [" + _FileGroup + "]");
            }
            //default binding
            foreach (_Column objColumn in _ColumnsCollection)
            {
                if (objColumn.DefaultBinding != "")
                {
                    strSQL.Append("EXEC sp_bindefault '" + objColumn.DefaultBinding + "', '" + this.GetObjectName().Replace("'", "''") + ".[" + objColumn.Name.Replace("'", "''") + "]'");
                }
                if (objColumn.RuleBinding != "")
                {
                    strSQL.Append(";\n\r");
                    strSQL.Append(" EXEC sp_bindrule '" + objColumn.RuleBinding + "', '" + this.GetObjectName().Replace("'", "''") + ".[" + objColumn.Name.Replace("'", "''") + "]'");
                }
            }
            DataAccessProvider.Instance().ExecuteCommand(strSQL.ToString());
        }
        /// <summary>
        /// Load table Constraint
        /// </summary>
        /// <returns>Table Constraint list</returns>
        public DataTable GetConstraints()
        {
            StringBuilder _sqlCommand = new StringBuilder();
            _sqlCommand.Append(" SELECT sysconstraints.constid as id, OBJECT_NAME(sysconstraints.constid) as ConstName, 'PK' as ConstType FROM sysconstraints");
            _sqlCommand.Append(" WHERE sysconstraints.id=" + this.ID + " AND (sysconstraints.status & 0xf)=1");
            _sqlCommand.Append(" UNION");

            _sqlCommand.Append(" SELECT sysconstraints.constid as id, OBJECT_NAME(sysconstraints.constid) as ConstName, 'UK' as ConstType FROM sysconstraints");
            _sqlCommand.Append(" WHERE sysconstraints.id=" + this.ID + " AND (sysconstraints.status & 0xf)=2");
            _sqlCommand.Append(" UNION");

            _sqlCommand.Append(" SELECT sysconstraints.constid as id, OBJECT_NAME(sysconstraints.constid) as ConstName, 'CC' as ConstType FROM sysconstraints");
            _sqlCommand.Append(" WHERE sysconstraints.id=" + this.ID + " AND (sysconstraints.status & 0xf)=4");
            _sqlCommand.Append(" UNION");
            _sqlCommand.Append(" SELECT sysconstraints.constid as id, OBJECT_NAME(sysconstraints.constid) as ConstName, 'FK' as ConstType FROM sysconstraints");
            _sqlCommand.Append(" WHERE sysconstraints.id=" + this.ID + " AND ((sysconstraints.status & 0x3)=0x3)");

            return DataAccessProvider.Instance().ReturnDataSet(_sqlCommand.ToString());
        }
        /// <summary>
        /// load table dependencies
        /// </summary>
        /// <returns>Dependencies list</returns>
        public DataTable GetDependencies()
        {
            StringBuilder _sqlCommand = new StringBuilder();

            _sqlCommand.Append(" SELECT      dbo.sysobjects.id as id, dbo.sysobjects.name AS name, dbo.sysobjects.xtype AS type, 'Ref' AS direction");
            _sqlCommand.Append(" FROM        dbo.sysobjects INNER JOIN dbo.sysdepends ON dbo.sysobjects.id = dbo.sysdepends.id");
            _sqlCommand.Append(" WHERE       (dbo.sysdepends.depid = OBJECT_ID('" + this.GetObjectName().Replace("'", "''") + "')) AND (dbo.sysobjects.type <> 'C')");

            _sqlCommand.Append(" UNION");

            _sqlCommand.Append(" SELECT      dbo.sysobjects.id as id, dbo.sysobjects.name AS name, 'U' AS type, 'Ref' AS direction");
            _sqlCommand.Append(" FROM        dbo.sysobjects INNER JOIN dbo.sysreferences ON dbo.sysobjects.id = dbo.sysreferences.fkeyid");
            _sqlCommand.Append(" WHERE       (dbo.sysreferences.rkeyid = OBJECT_ID('" + this.GetObjectName().Replace("'", "''") + "'))");

            _sqlCommand.Append(" UNION");

            _sqlCommand.Append(" SELECT      dbo.sysobjects.id as id, name AS name, 'TR' AS type, 'Ref' AS direction");
            _sqlCommand.Append(" FROM        dbo.sysobjects");
            _sqlCommand.Append(" WHERE       (type = 'TR') AND (parent_obj = OBJECT_ID('" + this.GetObjectName().Replace("'", "''") + "'))");

            _sqlCommand.Append(" UNION");

            _sqlCommand.Append(" SELECT      dbo.sysindexes.indid as id, name AS name, 'I' AS type, 'Ref' AS direction");
            _sqlCommand.Append(" FROM        dbo.sysindexes");
            _sqlCommand.Append(" WHERE       (id = OBJECT_ID('" + this.GetObjectName().Replace("'", "''") + "')) AND (indid > 0) ");
            _sqlCommand.Append("            AND (indid < 255) AND (status & 2048 <> 2048) AND (status & 4096 <> 4096) ");
            _sqlCommand.Append("            AND (INDEXPROPERTY(id, name, 'IsStatistics') <> 1) AND (INDEXPROPERTY(id, name, 'IsAutoStatistics') <> 1) ");
            _sqlCommand.Append("            AND (INDEXPROPERTY(id, name, 'IsHypothetical') <> 1)");

            _sqlCommand.Append(" UNION");

            _sqlCommand.Append(" SELECT      dbo.syscolumns.id as id, S.name AS name, 'D' AS type, 'Dep' AS direction");
            _sqlCommand.Append(" FROM        dbo.syscolumns INNER JOIN");
            _sqlCommand.Append("            dbo.systypes S ON dbo.syscolumns.usertype = S.usertype INNER JOIN");
            _sqlCommand.Append("            dbo.systypes T ON S.type = T.type");
            _sqlCommand.Append(" WHERE       (dbo.syscolumns.id = OBJECT_ID('" + this.GetObjectName().Replace("'", "''") + "')) AND (dbo.syscolumns.usertype > 100) AND (T.usertype <> 18) AND (T.usertype < 100)");

            _sqlCommand.Append(" UNION");

            _sqlCommand.Append(" SELECT      dbo.sysobjects.id as id, OBJECT_NAME(dbo.sysreferences.rkeyid) AS name, 'U' AS type, 'Dep' AS direction");
            _sqlCommand.Append(" FROM        dbo.sysreferences INNER JOIN dbo.sysobjects ON dbo.sysreferences.rkeyid = dbo.sysobjects.id");
            _sqlCommand.Append(" WHERE       (dbo.sysreferences.fkeyid = OBJECT_ID('" + this.GetObjectName().Replace("'", "''") + "'))");

            return DataAccessProvider.Instance().ReturnDataSet(_sqlCommand.ToString());
        }        
        /// <summary>
        /// Drop table
        /// </summary>
        public void Drop()
        {
            string _sqlCommand = "DROP TABLE " + this.GetObjectName();
            DataAccessProvider.Instance().ExecuteCommand(_sqlCommand);
        }
        /// <summary>
        /// Rename table
        /// </summary>
        public void Rename(string strNewName)
        {
            string _sqlCommand = "EXEC sp_rename '" + this.GetObjectName().Replace("'", "''") + "','" + strNewName.Replace("'", "''") + "',OBJECT";
            DataAccessProvider.Instance().ExecuteCommand(_sqlCommand);
        }
    }
    /// <summary>
    /// Class wich define a table column
    /// </summary>
    public class _Column
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public _Column() { }

        #region "Variables"

        /// <summary>
        /// column name
        /// </summary>
        private string _Name;
        /// <summary>
        /// old column name for renaming
        /// </summary>
        private string _OldName;
        /// <summary>
        /// Column type
        /// </summary>
        private string _Type;
        /// <summary>
        /// column width
        /// </summary>
        private string _Width;
        /// <summary>
        /// column scale
        /// </summary>
        private string _Scale;
        /// <summary>
        /// check if column is identity
        /// </summary>
        private bool _IsIdentity;
        /// <summary>
        /// identity seed
        /// </summary>
        private string _Seed;
        /// <summary>
        /// identity increment
        /// </summary>
        private string _Increment;
        /// <summary>
        /// default constraint value
        /// </summary>
        private string _Default;
        /// <summary>
        /// default binding
        /// </summary>
        private string _DefaultBinding;
        /// <summary>
        /// default constaint name
        /// </summary>
        private string _DefaultConstraint;
        /// <summary>
        /// Check if column allow nulls
        /// </summary>
        private bool _AllowNulls;
        /// <summary>
        /// check if column is primary ky
        /// </summary>
        private bool _IsPrimaryKey;
        /// <summary>
        /// Column precision
        /// </summary>
        private string _Precision;
        /// <summary>
        /// column size
        /// </summary>
        private string _Size;
        /// <summary>
        /// column id
        /// </summary>
        private string _ColId;
        /// <summary>
        /// Rule name
        /// </summary>
        private string _RuleBinding;


        #endregion

        #region "Properties"

        /// <summary>
        /// Allow nulls
        /// </summary>
        public bool AllowNulls
        {
            get
            {
                return _AllowNulls;
            }
            set
            {
                _AllowNulls = value;
            }
        }

        /// <summary>
        /// Default value
        /// </summary>
        public string Default
        {
            get
            {
                return _Default;
            }
            set
            {
                _Default = value;
            }
        }

        /// <summary>
        /// Default object name
        /// </summary>
        public string DefaultBinding
        {
            get
            {
                return _DefaultBinding;
            }
            set
            {
                _DefaultBinding = value;
            }
        }

        /// <summary>
        /// Default constraint
        /// </summary>
        public string DefaultConstraint
        {
            get
            {
                return _DefaultConstraint;
            }
            set
            {
                _DefaultConstraint = value;
            }
        }

        /// <summary>
        /// Identity increment
        /// </summary>
        public string Increment
        {
            get
            {
                return _Increment;
            }
            set
            {
                _Increment = value;
            }
        }

        /// <summary>
        /// Identity check
        /// </summary>
        public bool IsIdentity
        {
            get
            {
                return _IsIdentity;
            }
            set
            {
                _IsIdentity = value;
            }
        }

        /// <summary>
        /// check if column is primary key
        /// </summary>
        public bool IsPrimaryKey
        {
            get
            {
                return _IsPrimaryKey;
            }
            set
            {
                _IsPrimaryKey = value;
            }
        }

        /// <summary>
        /// Column width
        /// </summary>
        public string Width
        {
            get
            {
                return _Width;
            }
            set
            {
                _Width = value;
            }
        }

        /// <summary>
        /// Column name
        /// </summary>
        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }

        /// <summary>
        /// Column old name, for renaming
        /// </summary>
        public string OldName
        {
            get
            {
                return _OldName;
            }
            set
            {
                _OldName = value;
            }
        }

        /// <summary>
        /// Column precision
        /// </summary>
        public string Precision
        {
            get
            {
                return _Precision;
            }
            set
            {
                _Precision = value;
            }
        }

        /// <summary>
        /// Column scale
        /// </summary>
        public string Scale
        {
            get
            {
                return _Scale;
            }
            set
            {
                _Scale = value;
            }
        }

        /// <summary>
        /// Identity seed
        /// </summary>
        public string Seed
        {
            get
            {
                return _Seed;
            }
            set
            {
                _Seed = value;
            }
        }

        /// <summary>
        /// column size
        /// </summary>
        public string Size
        {
            get
            {
                return _Size;
            }
            set
            {
                _Size = value;
            }
        }

        /// <summary>
        /// Column type
        /// </summary>
        public string Type
        {
            get
            {
                return _Type;
            }
            set
            {
                _Type = value;
            }
        }

        /// <summary>
        /// column id
        /// </summary>
        public string ColId
        {
            get
            {
                return _ColId;
            }
            set
            {
                _ColId = value;
            }
        }

        /// <summary>
        /// Rule name
        /// </summary>
        public string RuleBinding
        {
            get
            {
                return _RuleBinding;
            }
            set
            {
                _RuleBinding = value;
            }
        }
        #endregion
    }
    /// <summary>
    /// Columns manage class
    /// </summary>
    public class Columns : CollectionBase
    {
        /// <summary>
        /// Columns collection
        /// </summary>
        /// <param name="item">Item name</param>
        /// <returns>Column pbject</returns>
        public _Column this[int item]
        {
            get
            {
                return this.GetItem(item);
            }
        }

        /// <summary>
        /// Add column to the collection
        /// </summary>
        /// <param name="newItem">column object</param>
        public void Add(_Column newItem)
        {
            List.Add(newItem);
        }

        /// <summary>
        /// Remove column from the collection
        /// </summary>
        /// <param name="index">Column index</param>
        /// <returns>True - False object removed</returns>
        public bool Remove(int index)
        {
            if (index > Count - 1 || index < 0)
            {
                return false;
            }
            else
            {
                List.RemoveAt(index);
                return true;
            }
        }

        /// <summary>
        /// Get specified column
        /// </summary>
        /// <param name="Index">column index</param>
        /// <returns>Column object</returns>
        private _Column GetItem(int Index)
        {
            return (_Column)List[Index];
        }

    }
}
